package com.easy.order.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.easy.common.bean.Order;
import com.easy.common.bean.OrderItems;
import com.easy.common.enumbean.OrderStatus;
import com.easy.common.service.IOrderService;
import com.easy.order.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.Objects;

@Service
public class OrderService implements IOrderService {
    @Autowired
    OrderMapper orderMapper;
    @Override
    public Order insert(Order order) {
        orderMapper.insert(order);
        Order result=this.getByID(order.getId());
        return result;
    }

    @Override
    public Order update(Order order) {
        orderMapper.updateById(order);
        Order result=this.getByID(order.getId());
        return result;
    }

    @Override
    public Order transact(Integer id) {
        Order order = orderMapper.selectById(id);
        if (Objects.nonNull(order)) {
            UpdateWrapper<Order> wrapper = new UpdateWrapper<>();
            wrapper.set("order_status", OrderStatus.TRANSACTED).eq("id", id);
            orderMapper.update(null, wrapper);
            return orderMapper.selectById(id);
        }
        return null;
    }
    @Override
    public Order cancel(Integer id) {
        Order order = orderMapper.selectById(id);
        if (Objects.nonNull(order)) {
            UpdateWrapper<Order> wrapper = new UpdateWrapper<>();
            wrapper.set("order_status", OrderStatus.CANCELLED).eq("id", id);
            orderMapper.update(null, wrapper);
            return orderMapper.selectById(id);
        }
        return null;
    }
    @Override
    public Order getByID(Integer id) {
        Order result=orderMapper.selectById(id);
        return result;
    }

    @Override
    public IPage<Order> getListByUserID(IPage page, Integer userid) {
        Wrapper wrapper=new QueryWrapper(userid);
        IPage result=orderMapper.selectPage(page,wrapper);
        return result;
    }

    @Override
    public IPage<Order> getListByCustomerID(IPage page, Integer customerid) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("customer_id", customerid);
        IPage<Order> result = orderMapper.selectPage(page, wrapper);
        return result;
    }

    @Override
    public OrderItems insertItem(OrderItems orderItems) {
        orderMapper.insertOrderItem(orderItems);
        return orderMapper.selectOrderItemById(orderItems.getId());
    }

    @Override
    public OrderItems updateItem(OrderItems orderItems) {
        orderMapper.updateOrderItemById(orderItems);
        return orderMapper.selectOrderItemById(orderItems.getId());
    }

    @Override
    public int deleteItem(Integer id) {
        return orderMapper.deleteOrderItemById(id);
    }
}
